<template>
	<view>
		<head-navbar :back="true" class="headNavbar" :bgColor="'#FE5900'">
			<view>
				<text style="color: #FFFFFF;">分组管理</text>
			</view>
		</head-navbar>
		<view class="list">
			<view class="additem" @click="addGruop">
				<image src="../../../static/img/icon_group.png"></image>
				<view class="addtext">添加分组</view>
			</view>
			<view class="item" v-for="(item,index) in list" >
				<radio v-if="!item.status" color="#FE5900" class="rido" :checked="item.checked" @click="selectFun(index)"/>
				<view class="title" v-if="!item.status">{{item.name}}</view>
			</view>
		</view>
		<view class="btn">
			<button @click="onJoin">完成</button>
		</view>

		<u-popup v-model="show" class="prop" mode="center" border-radius="14" length="60%">
			<view class="title">添加分组</view>
			<view class="input"><input :focus="show" type="text" v-model="gropName" /></view>
			<view class="propBtn">
				<view class="cancel" @click="cancel">取消</view>
				<view class="commit" @click="commit">确认</view>
			</view>
		</u-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		createGroup,
		getGroupList,
		upDateGoodsGroup
	} from '@/api/order/index.js';
	export default {
		data() {
			return {
				gropName: "",
				show: false,
				list: [
					// {name:'分组1',status:false}
				],
				goodsIdList:[],
				groupId:''
			}
		},
		onLoad(option){
			let list = option.list;
			this.goodsIdList = list.split(',');
		},
		onShow() {
			this.getGroup();
		},
		methods: {
			selectFun(index) {
				if(this.list[index].checked) {
					this.list[index].checked = false;
				}else{
					this.list[index].checked = true;
					this.groupId = this.list[index].id
				}
				
			},
			addGruop() {
				this.show = true
				this.gropName = ''
			},
			async getGroup() {
				try{
					const res = await getGroupList();
					let list = res.data.obj
					list.forEach(e=>{
						e.status = false;
						e.name = e.groupName;
						e.checked = false;
					})
					this.list = list;
				}catch(error){
					
				}
			},
			// 确定
			async commit() {
				let parmas = {
					groupName: this.gropName,
					goodsIds: [] //商品数组
				}
				try {
					const ret = await createGroup(parmas)
					this.show = false;
					this.$refs.uToast.show({
						title: ret.data.msg,
						type: 'success'
					})
					this.getGroup();
				} catch (error) {
					this.$refs.uToast.show({
						title: error.data.msg,
						type: 'error'
					})
					this.getGroup();
				}
			},
			cancel() {
				this.show = false
			},
			toEditer(index) {
				this.list[index].status = !this.list[index].status
			},
			async onJoin() {
				let parmas = {
					groupId:this.groupId,
					goodsIds:this.goodsIdList
				}
				console.log(parmas)
				try {
					const ret= await upDateGoodsGroup(parmas);
					this.$refs.uToast.show({
						title: ret.data.msg
					})
					uni.navigateBack({
						delta: 1
					})
				}catch(error) {
					this.$refs.uToast.show({
						title: error.data.msg,
						type: 'error'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.prop {
		margin-top: -100rpx;
		padding: 20rpx;

		.title {
			font-size: 35rpx;
			font-weight: 600;
			padding: 20rpx;
			text-align: center;
		}

		.input {
			padding: 20rpx;
			border-bottom: 1px solid #f4f4f4;
		}

		.propBtn {
			display: flex;

			.cancel,
			.commit {
				flex: 1;
				text-align: center;
				padding: 20rpx;
			}

			.cancel {
				color: #949494;
			}

			.commit {
				color: #FE5900;
			}
		}
	}

	.btn {
		height: 130rpx;
		box-shadow: 0 0 10rpx #E0E0E0;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 99;
		background: #FFFFFF;

		button {
			width: 85%;
			height: 80rpx;
			line-height: 80rpx;
			background: #FE5900;
			color: #FFFFFF;
			border-radius: 100rpx;
		}
	}

	.list {
		padding-bottom: 130rpx;

		.additem {
			height: 100rpx;
			padding: 0 20rpx;
			background: #FFFFFF;
			border-bottom: 1px solid #f4f4f4;
			display: flex;
			align-items: center;
			color: #949494;

			image {
				height: 40rpx;
				width: 40rpx;
				margin-right: 20rpx;
			}
		}

		.item {
			padding: 20rpx;
			background: #FFFFFF;
			border-bottom: 1px solid #f4f4f4;
			display: flex;
			align-items: center;

			.title {
				flex: auto;
				margin: 0 20rpx;
			}
		}
	}
</style>
